import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


class SelectionScree extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Pick an option'),),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(padding: EdgeInsets.all(8.0),
            child: ElevatedButton(
              onPressed: (){
                Navigator.pop(context, 'Yep!');
              },
              child: Text('Yep!'),
            ),),
            Padding(padding: EdgeInsets.all(8.0),
            child: ElevatedButton(
              onPressed: (){
                Navigator.pop(context, 'Nope.');
              },
              child: Text('Nope.'),
            ),)
          ],
        ),
      ),
    );
  }
}

class SelectionButton extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(onPressed: (){
      _navigateAndDisplaySelection(context);
    }, child: Text('Pick an option, any option!'));
  }

  //通过异步的方式获取返回的值
  _navigateAndDisplaySelection(BuildContext context) async {
    final result = await Navigator.push(context, MaterialPageRoute(builder: (context) => SelectionScree()));

    ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('$result')));
  }
}

class HomeScreen extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: AppBar(title: Text('Returning Data Demo'),),
    body: Center(child: SelectionButton(),),);
  }
}


class NavigatorPopEx extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return HomeScreen();
  }
}
